package com.zhiwang.planet.mvp.p

import com.zhiwang.planet.di.ActivityScope
import com.zhiwang.planet.mvp.v.PceggView
import com.zhiwang.planet.mvvm.model.RestApi
import com.zhiwang.planet.mvvm.model.StringHolder
import com.zhiwang.planet.mvvm.model.entity.main.ServiceQQ
import com.zhiwang.planet.mvvm.model.request.LaunchRequestBody
import com.zhiwang.planet.util.TokenManager
import com.zhiwang.planet.util.androidSchedule
import com.zhiwang.planet.util.responseFunc
import javax.inject.Inject

@ActivityScope
class SdkPresenter @Inject constructor(var restApi: RestApi) {

    lateinit var pceggView: PceggView

    fun getPceggPageUrl() {
        val disposable = restApi.getPceggPageUrl()
                .androidSchedule()
                .subscribe({
                    StringHolder.sdkUrl = it.string()
                }, {

                })

    }

    fun getPceggUrl() {
        val disposable = restApi.getPceggPageUrl()
                .androidSchedule()
                .subscribe({
                    StringHolder.sdkUrl = it.string()
                    pceggView.onPceggUrlLoad(StringHolder.sdkUrl)
                }, {

                })
    }

    fun getServiceQQ(onSuccess: (ServiceQQ) -> Unit, onError: (Throwable) -> Unit) {
        val subscribe = restApi.getServiceQQ()
                .androidSchedule()
                .responseFunc()
                .subscribe({
                    onSuccess(it)
//                    TokenManager.saveServiceQQ(it.qq)
//                    TokenManager.saveQQKey(it.qqKey)
                }, {
                    //no op
                    onError(it)
                }, {

                })
    }

    fun reportAppLaunch(channelId: String, deviceid: String) {
        val subscribe = restApi.reportLaunchLog(LaunchRequestBody(channelId, deviceid))
                .androidSchedule()
                .subscribe({

                }, {

                }, {

                })

    }


}